﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GeekShop
{
    public partial class MarkerOptions : UserControl
    {
        #region Events
        public event MarkerColorChangedHandler ColorChanged;
        public event MarkerDeletedHandler MarkerDeleted;
        public event MarkerMovedHandler MarkerMoved;
        #endregion

        #region Fields
        private int index;
        private Marker marker;
        #endregion

        #region Properties
        public int Index
        {
            get { return this.index; }
            set
            {
                if (value < 0)
                {
                    this.index = 0;
                }
                else if (value > 16777215)
                {
                    this.index = 16777215;
                }
                else
                {
                    this.index = value;
                }
                this.textIndex.Text = this.index.ToString();
            }
        }
        public Marker Marker
        {
            get { return this.marker; }
            set
            {
                if (value != null)
                {
                    this.marker = value;
                    this.panelColor.BackColor = value.Color;
                    this.textAlpha.Text = ((int)(value.Color.A * 100 / 255)).ToString();
                }
            }
        }
        #endregion

        #region Constructors
        public MarkerOptions()
        {
            InitializeComponent();
            
        }
        #endregion

        #region Private Methods
        private void UpdateIndex()
        {
            MarkerEventArgs m = new MarkerEventArgs();
            m.OldIndex = this.Index;
            m.OldMarker = this.Marker;
            this.Index = int.Parse(this.textIndex.Text);
            this.textIndex.Text = this.Index.ToString();
            m.NewIndex = this.Index;
            m.NewMarker = this.Marker;
            this.MarkerMoved(this, m);
        }
        private void UpdateAlpha()
        {
            int alpha = int.Parse(this.textAlpha.Text);
            if (alpha < 0)
            {
                alpha = 0;
                this.textAlpha.Text = alpha.ToString();
            }
            else if (alpha > 100)
            {
                alpha = 100;
                this.textAlpha.Text = alpha.ToString();
            }
            else
            {
                MarkerEventArgs m = new MarkerEventArgs();
                m.OldIndex = this.Index;
                m.OldMarker = this.marker;
                Color newColor = Color.FromArgb((int)(alpha * 255 / 100), this.Marker.Color);
                this.marker = new Marker(newColor);
                this.panelColor.BackColor = newColor;
                m.NewMarker = this.marker;
                this.ColorChanged(this, m);
            }
        }
        #endregion

        #region Event Methods
        private void panelColor_MouseClick(object sender, MouseEventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                MarkerEventArgs m = new MarkerEventArgs();
                m.OldMarker = this.marker;
                m.OldIndex = this.Index;
                this.Marker = new Marker(colorDialog1.Color);
                m.NewMarker = this.marker;
                m.NewIndex = this.Index;
                this.ColorChanged(this, m);
            }
        }
        private void btDelete_Click(object sender, EventArgs e)
        {
            MarkerEventArgs m = new MarkerEventArgs();
            m.OldIndex = this.Index;
            m.OldMarker = this.Marker;
            this.MarkerDeleted(this, m);
        }
        private void textIndex_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.UpdateIndex();
            }
        }
        private void textIndex_Leave(object sender, EventArgs e)
        {
            this.UpdateIndex();
        }
        private void textAlpha_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.UpdateAlpha();
            }
        }
        private void textAlpha_Leave(object sender, EventArgs e)
        {
            this.UpdateAlpha();
        }
        #endregion
    }
}
